package com.eshore.thrpc.client.thrift;

import org.apache.thrift.TServiceClient;
import org.apache.thrift.TServiceClientFactory;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import com.eshore.thrpc.client.IClient;

/**
 * 
 * 非阻塞客户端
 * 
 *   
 * @author zhouzhixiang  
 * @date 2016年12月12日 上午9:47:55 
 * @since 1.0
 */
public class ThriftNonBlockingClient implements IClient{
	/** {@link TServiceClientFactory }<{@link TServiceClient}> */
    private TServiceClientFactory<TServiceClient> clientFactory;
    
    public ThriftNonBlockingClient(TServiceClientFactory<TServiceClient> clientFactory){
    	this.clientFactory = clientFactory;
    }
    
	@Override
	public TServiceClient getClient(String ip,int port,int timeout) throws Exception {
		TSocket tsocket = new TSocket(ip, port, timeout);
		TTransport transport = new TFramedTransport(tsocket);
		TCompactProtocol protocol = new TCompactProtocol(transport); 
		TServiceClient client = clientFactory.getClient(protocol);
		transport.open();
		return  client;
	}
	
}
